#include "qmladvancedanalysisconfig.h"

QMLAdvancedAnalysisConfig::QMLAdvancedAnalysisConfig()
{
    this->resize(900, 512);
    this->setResizeMode(QQuickWidget::SizeRootObjectToView);
    this->setSource(QUrl("qrc:/qmlComponents/AdvancedAnalysisConfig.qml"));

    _rootContext = this->rootContext();
    _rootObject = (QObject*)this->rootObject();

    _rootContext->setContextProperty("CConfig", this);


    if(_rootObject){

    }else{
        qDebug() << __TIME__ << "!Error: ConstructQmlFalse-" << __FUNCTION__;
    }

    QMap<QString, double> defaultConfig;
    defaultConfig["FIXED_BASE_END"] = -1;
    defaultConfig["CLIP_START"] =     5;
    defaultConfig["SLOPE_THR"] =      2.8;
    defaultConfig["SLOPE_THR_DOWN"] = 0.6;
    defaultConfig["MIN_CONF"] =       0.9;
    defaultConfig["RANGE_THR"] =      1000;
    _channelConfigList["FAM"] = defaultConfig;
    _channelConfigList["VIC"] = defaultConfig;
    _channelConfigList["ROX"] = defaultConfig;
    _channelConfigList["CY5"] = defaultConfig;
}


void QMLAdvancedAnalysisConfig::reanalysis(QString target, QVariantMap params){
    QMap<QString, double> paramsMap;
    for(auto iter = params.begin(); iter != params.end(); iter ++){
        paramsMap[iter.key()] = iter.value().toDouble();
    }

    if(!target.contains("_")){
        _channelConfigList[target] = paramsMap;
    }

    emit sigReanalysis(target, paramsMap);
}
QVariantMap QMLAdvancedAnalysisConfig::getParams(QString target){

    QVariantMap res;
    if(target.contains("_")){
        if(_curveConfigList.contains(target)){
            for(auto iter = _curveConfigList[target].begin(); iter != _curveConfigList[target].end(); iter ++){
                res[iter.key()] = iter.value();
            }
        }
        return res;
    }else{
        if(_channelConfigList.contains(target)){
            for(auto iter = _channelConfigList[target].begin(); iter != _channelConfigList[target].end(); iter ++){
                res[iter.key()] = iter.value();
            }
        }return res;
    }
    return res;
}
QVariantMap QMLAdvancedAnalysisConfig::getDefaultParams(){

    QVariantMap res;
    res["FIXED_BASE_END"] = -1;
    res["CLIP_START"] =     5;
    res["SLOPE_THR"] =      2.8;
    res["SLOPE_THR_DOWN"] = 0.6;
    res["MIN_CONF"] =       0.9;
    res["RANGE_THR"] =      1000;
    return res;
}

void QMLAdvancedAnalysisConfig::setBoldCurveList(QStringList list){
    emit sigSelectCurveChanged(list);
}

void QMLAdvancedAnalysisConfig::setParamsList(QMap<QString, QMap<QString, double> > list){
    _curveConfigList = list;
}

void QMLAdvancedAnalysisConfig::setSelectableList(QStringList list){
    _selectablePosiList = list;
    emit selectablePosiListChanged(_selectablePosiList);
}
